package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.entity.ApAuthor;
import com.heima.article.feign.ArticleFeign;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.entity.WmUser;
import com.heima.media.feign.MediaFeign;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-28
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult listByStatus(AuthDto authDto) {
        Page<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if(authDto.getStatus()!=null){
            queryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        this.page(page,queryWrapper);
        return new PageResponseResult(authDto.getPage(),authDto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {

        ApUserRealname apUserRealname = this.getById(dto.getId());
        //0代表驳回则需要将原因返回
        if(type==0){
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
            return ResponseResult.okResult("审核被驳回");
        }
        //审核成功则需要更新apuser realname 表，并远程调用微服务进行保存
        //更新apUserRealname表
        apUserRealname.setStatus(9);
        this.updateById(apUserRealname);
        //更新apUser表
        LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUser::getId,apUserRealname.getUserId());
        ApUser apUser = apUserService.getOne(queryWrapper);
        apUser.setIsIdentityAuthentication(true);
        apUser.setFlag(1);
        apUserService.updateById(apUser);
        //更新
        WmUser wmUser = saveWmuser(apUser);
        ApAuthor apAuthor = saveAuthor(wmUser);
        wmUser.setApAuthorId(apAuthor.getId());
        mediaFeign.updateWmUser(wmUser);
        return ResponseResult.okResult();
    }



    private WmUser saveWmuser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setScore(0);
        wmUser.setStatus(9);
        wmUser.setType(0);
        wmUser.setApUserId(apUser.getId());
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        WmUser user = responseResult.getData();
        return user;
    }
    private ApAuthor saveAuthor(WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(wmUser.getName());

        apAuthor.setType(2);
        apAuthor.setUserId(wmUser.getApUserId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        ApAuthor resultData = responseResult.getData();
        return resultData;
    }
}
